package com.workflow.action;

import java.util.List;
import java.util.Set;

import com.base.service.CardTypeService;
import com.base.service.DepartmentService;
import com.base.service.DictService;
import com.base.service.FlowworkitemService;
import com.base.service.PermissionService;
import com.common.bean.Cardmaintype;
import com.common.bean.Datadict;
import com.common.bean.Department;
import com.common.bean.Flowtemplatedetail;
import com.common.bean.Flowworkitem;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Applydept;
import com.workflow.bean.Outputdept;
import com.workflow.service.ApplydeptService;
import com.workflow.service.OutputdeptService;

public class OutputdeptManagerAction extends AbstractActionSupport implements
		Preparable {
	private static final long serialVersionUID = -3868036642438188055L;

	private OutputdeptService outputdeptService;
	
	private ApplydeptService applydeptService;//获取报领单数据、自动生成出库单
	
	private DepartmentService departmentService;
	
	private DictService dictService;
	
	private FlowworkitemService flowworkitemService;
	
	private CardTypeService cardTypeService;
	
	private PermissionService permissionService;
	
	private Outputdept outputdept;
	
	private Applydept applydept;
	
	private String outputno;
	
	private String applyno;//报领单号
	
	private PagerModel pm;
	
	private List<Department> departmentByDepartmentDeptcodelist;
	
	private List<Datadict> datadictByMarklist;
	
	private List<Datadict> datadictByUnitlist;
	
	private List<Datadict> datadictByPrilist;
	
	private List<Datadict> datadictByStatuslist;
	
	private List<Cardmaintype> cardmaintypelist;
	
	@SuppressWarnings("unchecked")
	public void prepare() throws Exception {
		departmentByDepartmentDeptcodelist = departmentService.findAllleafDepartment();
		cardmaintypelist = cardTypeService.findAllCardmaintype();
		datadictByUnitlist = dictService.findDictDetail(StringToken.DICT_WORK_UNIT);
		datadictByPrilist = dictService.findDictDetail(StringToken.DICT_WORK_PRI);
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddOutputdept() throws Exception {
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addOutputdept() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		Flowworkitem flowworkitem = null;
		/*
		 admin 账户为超级管理员用户！测试专用。		 
		 */
		String deptcode = "";
		Department department = new Department();
		if("admin".equals(personnel.getLoginname())){
			deptcode = "0002"; //测试部门 TODO
			department.setDeptcode(deptcode);
		}else {
			deptcode = personnel.getDepartment().getDeptcode();
			department.setDeptcode(deptcode);
		}
		flowworkitem = flowworkitemService.finaFlowworkitem(deptcode
				, outputdept.getCardmaintype().getCardmaintypeno()/*类型一致*/
				, StringToken.DICT_FLOWFLAG_OUTPUTDEPT);
		
		outputdept.setPersonnel(personnel);
		outputdept.setDepartmentByDeptcode(department);
		outputdept.setFlowworkitem(flowworkitem);
		
		Datadict datadictByMark = new Datadict();
		datadictByMark.setTypeid(StringToken.DICT_WORK_MARK_NO);
		outputdept.setDatadictByMark(datadictByMark);
		
		Object obj = null;
		Set<Flowtemplatedetail>	set = flowworkitem.getFlowtemplatedetails();
		for(Flowtemplatedetail flowtemplatedetail : set) {
			if(flowtemplatedetail.getSeq() == 1l) {
				//判断用户权限部门、角色
				if(flowtemplatedetail.getDepartment().equals(personnel.getDepartment())) {
					long ruleno = flowtemplatedetail.getRule().getRuleno();
					String staffcode = personnel.getStaffcode();
					
					obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
				}
				
				outputdept.setFlowtemplatedetail(flowtemplatedetail);//默认1为保存，从1开始
				outputdept.setDatadictByStatus(flowtemplatedetail.getDatadictByWorkflowstatusno());//保存状态,默认
				outputdept.setDatadictByBranch(flowtemplatedetail.getDatadictByBranch());//是否分支 :流程1时，默认取1分支
			}
		}
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		outputdeptService.addOutputdept(outputdept);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addOutputdeptByApplyno() throws Exception {
		outputdept = new Outputdept();
		applydept = applydeptService.findApplydept(applyno);
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		Flowworkitem flowworkitem = null;
		
		/*
		 admin 账户为超级管理员用户！测试专用。		 
		 */
		Department department = null;
		if("admin".equals(personnel.getLoginname())){
			department = applydept.getDepartmentByDepartmentDeptcode();
		}else {
			department = personnel.getDepartment();
		}
		flowworkitem = flowworkitemService.finaFlowworkitem(department.getDeptcode()
				, applydept.getCardmaintype().getCardmaintypeno()/*类型一致*/
				, StringToken.DICT_FLOWFLAG_OUTPUTDEPT);
		
		outputdept.setPersonnel(personnel);
		outputdept.setDepartmentByDeptcode(department);
		outputdept.setFlowworkitem(flowworkitem);
		
		Object obj = null;
		Set<Flowtemplatedetail>	set = flowworkitem.getFlowtemplatedetails();
		for(Flowtemplatedetail flowtemplatedetail : set) {
			if(flowtemplatedetail.getSeq() == 1l) {
				//判断用户权限部门、角色
				if(flowtemplatedetail.getDepartment().equals(personnel.getDepartment())) {
					long ruleno = flowtemplatedetail.getRule().getRuleno();
					String staffcode = personnel.getStaffcode();
					
					obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
				}
				
				outputdept.setFlowtemplatedetail(flowtemplatedetail);//默认1为保存，从1开始
				outputdept.setDatadictByStatus(flowtemplatedetail.getDatadictByWorkflowstatusno());//保存状态,默认
				outputdept.setDatadictByBranch(flowtemplatedetail.getDatadictByBranch());//是否分支 :流程1时，默认取1分支
			}
		}
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		//判断单子是否过期（完成状态）
		//是否已经存在相应的出库单(单子正在流转或完成)
		if(!"完成".equals(applydept.getDatadictByStatus().getName())) {
			throw new RuntimeException("流程未完成！");
		}
		
		outputdept.setCardmaintype(applydept.getCardmaintype());
		outputdept.setDepartmentByDeptcode(department);
		outputdept.setDepartmentByDepartmentDeptcode(applydept.getDepartmentByDeptcode());
		outputdept.setDatadictByUnit(applydept.getDatadictByUnit());
		outputdept.setDatadictByPri(applydept.getDatadictByPri());
		outputdept.setDatadictByMark(applydept.getDatadictByMark());
		outputdept.setLimitday(applydept.getLimitday());
		outputdept.setPersonnel(personnel);
		outputdept.setUpperflowno(applydept.getApplyno());
		
		outputdeptService.addOutputdept(outputdept);
		
		Datadict datadictByStatus = new Datadict();
		datadictByStatus.setTypeid(StringToken.DICT_WORK_STATUS_CANCEL);
		applydept.setDatadictByStatus(datadictByStatus);
		applydeptService.updateApplydept(applydept);
		return SUCCESS;
	}
	
	public String findAllOutputdept() throws Exception {
		pm = outputdeptService.findOutputdeptForPager();
		return SUCCESS;
	}
	
	public String findOutputdept() throws Exception {
		outputdept = outputdeptService.findOutputdept(outputno);
		return SUCCESS;
	}
	
	public String delOutputdept() throws Exception {
		outputdeptService.removeOutputdept(outputno);
		return SUCCESS;
	}
	
	public String getOutputdeptForUpdate() throws Exception {
		outputdept = outputdeptService.findOutputdept(outputno);
		return SUCCESS;
	}
	
	public String updateOutputdept() throws Exception {
		Outputdept output = null;
		output = outputdeptService.findOutputdept(outputdept.getOutputno());
		
		if(output == null ) {
			throw new RuntimeException("has found no instance ");
		}
		output.setDatadictByUnit(outputdept.getDatadictByUnit());
		output.setDatadictByPri(outputdept.getDatadictByPri());
		output.setDepartmentByDepartmentDeptcode(outputdept.getDepartmentByDepartmentDeptcode());
		output.setLimitday(outputdept.getLimitday());
		output.setRemark(outputdept.getRemark());
		outputdeptService.updateOutputdept(outputdept);
		return SUCCESS;
	}

	public List<Cardmaintype> getCardmaintypelist() {
		return cardmaintypelist;
	}

	public void setCardmaintypelist(List<Cardmaintype> cardmaintypelist) {
		this.cardmaintypelist = cardmaintypelist;
	}

	public CardTypeService getCardTypeService() {
		return cardTypeService;
	}

	public void setCardTypeService(CardTypeService cardTypeService) {
		this.cardTypeService = cardTypeService;
	}

	public List<Datadict> getDatadictByMarklist() {
		return datadictByMarklist;
	}

	public void setDatadictByMarklist(List<Datadict> datadictByMarklist) {
		this.datadictByMarklist = datadictByMarklist;
	}

	public List<Datadict> getDatadictByPrilist() {
		return datadictByPrilist;
	}

	public void setDatadictByPrilist(List<Datadict> datadictByPrilist) {
		this.datadictByPrilist = datadictByPrilist;
	}

	public List<Datadict> getDatadictByStatuslist() {
		return datadictByStatuslist;
	}

	public void setDatadictByStatuslist(List<Datadict> datadictByStatuslist) {
		this.datadictByStatuslist = datadictByStatuslist;
	}

	public List<Datadict> getDatadictByUnitlist() {
		return datadictByUnitlist;
	}

	public void setDatadictByUnitlist(List<Datadict> datadictByUnitlist) {
		this.datadictByUnitlist = datadictByUnitlist;
	}

	public List<Department> getDepartmentByDepartmentDeptcodelist() {
		return departmentByDepartmentDeptcodelist;
	}

	public void setDepartmentByDepartmentDeptcodelist(
			List<Department> departmentByDepartmentDeptcodelist) {
		this.departmentByDepartmentDeptcodelist = departmentByDepartmentDeptcodelist;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public DictService getDictService() {
		return dictService;
	}

	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}

	public FlowworkitemService getFlowworkitemService() {
		return flowworkitemService;
	}

	public void setFlowworkitemService(FlowworkitemService flowworkitemService) {
		this.flowworkitemService = flowworkitemService;
	}

	public Outputdept getOutputdept() {
		return outputdept;
	}

	public void setOutputdept(Outputdept outputdept) {
		this.outputdept = outputdept;
	}

	public OutputdeptService getOutputdeptService() {
		return outputdeptService;
	}

	public void setOutputdeptService(OutputdeptService outputdeptService) {
		this.outputdeptService = outputdeptService;
	}

	public String getOutputno() {
		return outputno;
	}

	public void setOutputno(String outputno) {
		this.outputno = outputno;
	}

	public PermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public PagerModel getPm() {
		return pm;
	}

	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the applydept
	 */
	public Applydept getApplydept() {
		return applydept;
	}

	/**
	 * @param applydept the applydept to set
	 */
	public void setApplydept(Applydept applydept) {
		this.applydept = applydept;
	}

	/**
	 * @return the applydeptService
	 */
	public ApplydeptService getApplydeptService() {
		return applydeptService;
	}

	/**
	 * @param applydeptService the applydeptService to set
	 */
	public void setApplydeptService(ApplydeptService applydeptService) {
		this.applydeptService = applydeptService;
	}

	/**
	 * @return the applyno
	 */
	public String getApplyno() {
		return applyno;
	}

	/**
	 * @param applyno the applyno to set
	 */
	public void setApplyno(String applyno) {
		this.applyno = applyno;
	}
	
}
